package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;

import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ICategoryService;
import com.sky.service.IDishService;
import com.sky.service.ISetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;

    @PostMapping
    @ApiOperation("新增分类")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.Mysave(categoryDTO);
        return Result.success();
    }


    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {

        //分页查询
        log.info("分类分页查询: {}", categoryPageQueryDTO);

        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);

    }

    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result deleteCategoryById(Long id) {
        log.info("根据id删除分类: {}", id);

        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        long count = dishService.count(wrapper1);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId, id);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }


        categoryService.removeById(id);
        return Result.success();
    }

    @ApiOperation("修改分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类: {}", categoryDTO);
        categoryService.Myupdate(categoryDTO);
        return Result.success();
    }

    @ApiOperation("启动禁用分类")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启动禁用分类: {},{}", status, id);
        categoryService.startOrStop(id, status);
        return Result.success();
    }


    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List> listByType(Integer type) {
        log.info("根据类型查询分类: {}", type);
        List categoryList = categoryService.listByType(type);
        return Result.success(categoryList);
    }

}
